home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10714 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.1 KB

  1. Path: maple.sover.net!mountain
  2. From: mountain@maple.sover.net (Steve Mount)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Converting Strings to Upper Case
  5. Date: 19 Mar 1996 16:39:27 GMT
  6. Organization: SoVerNet, Inc.
  7. Message-ID: <4imnvv$mu@thrush.sover.net>
  8. References: <4ifra6$52i@scipio.cyberstore.ca> <4ih7l3$526@thrush.sover.net> <4ihl4m$4ca@castle.nando.net>
  9. NNTP-Posting-Host: maple.sover.net
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Bill McCarthy (actuary@nando.net) wrote:
  13. : In <4ih7l3$526@thrush.sover.net>, mountain@sover.net (Steve Mount) writes:
  14. : >In article <4ifra6$52i@scipio.cyberstore.ca>, ejw@news.cyberstore.ca says...
  15. : >>I need to write a function to convert a string containg upper or lower case
  16. : >>characters to the opposite case.  Something like:
  17. : >>
  18. : >>  void libConvertUpperCase(char *str);  and
  19. : >>  void libConvertLowerCase(char *str);
  20. : >>
  21. : >>and the string would be modified.  I just can't seem to wrap my head around 
  22. : >>the best way that I know is better than writing a for loop to check each 
  23. : >>element in the array?
  24. : >
  25. : >My home compiler has strlwr and strupr functions.  At work, we don't, so
  26. : >just to make it work, quick and dirty, I did:
  27. : >
  28. : >void strnlwr(char *buffer, int len)
  29. : >{
  30. : >register int i;
  31. : >for (i=0;i<len;i++) buffer[i] = tolower(buffer[i]);
  32. : >return;
  33. : >}
  34.  
  35. : A couple of comments on this solution.  The inclusion of the parameter
  36. : len is problematic.  First the user must obtain a length before the call.
  37. : Second, not having a check in the function, conversion may continue
  38. : beyond the end of the string.
  39.  
  40. In our context, we always know the length.  If we wanna be sure, we use
  41. either with a sizeof call or a strlen call.  True, this is not a wonder-
  42. function - like I said, just something quick and dirty to get the job done.
  43. I've been working on other solutions for in-house use (I asked for comments
  44. in this newsgroup on my code... maybe you'll see it later in the topic 
  45. list), but this was code I'd cut and pasted many a time.
  46.  
  47. : Also the function returns nothing.  It is convenient to return a pointer.
  48.  
  49. Again, in my context, a return value was not needed.  My newer version does
  50. return a pointer to the buffer, just like memcpy and the like.
  51.  
  52. : Also, char may be signed and should therefore be cast before apply-
  53. : ing tolower().  Finally, it is always a good idea to declare a function
  54. : before using it.
  55.  
  56. I did not include the prototype in my post, but it's in the code.... somewhere.
  57.  
  58. : #include <ctype.h>
  59.  
  60. : char *mystr2lwr( char *s )
  61. : {
  62. :    char *t = s;
  63.  
  64. :    if ( t != NULL )
  65. :       while ( *s )
  66. :          *s++ = tolower( (unsigned char)*s );
  67.  
  68. :    return t;
  69. : }
  70.  
  71. This works great on null-terminated strings... but my function needed to work
  72. on any old buffer.
  73.  
  74. +============================================================================+
  75. | Steve Mount, Software Engineer            Work: sjjm@hawkeye.idx.com       |
  76. | CIS: 73720,3404  MSN: S_Mountain          Home: mountain@sover.net         |
  77. | AOL: Mountain                                                              |
  78. | WWW: http://www.sover.net/~mountain/      "Fight, Win, Prevail!"           |
  79. +============================================================================+
  80.